const apiUrl = "http://47.94.4.201/index.php/index/teacher/teacherlist";
let currentPage = 1;
const pageLimit = 10;

// 请求数据
async function fetchData(page) {
    if (page < 1) return;

    const response = await fetch(`${apiUrl}?page=${page}&pagelimit=${pageLimit}`);
    const result = await response.json();

    if (result.code === 0) {
        const dataList = result.data.data;
        const pageCount = result.data.pagecount;

        if (page > pageCount) return;

        currentPage = page;
        renderList(dataList);
        document.getElementById("current-page").innerText = currentPage;
        document.getElementById("total-pages").innerText = Math.ceil(pageCount / 8);
    }
}
// ---------------------讲师管理列表-----------------------
function renderList(data) {
    const dataListDiv = document.getElementById("data-list");
    function app(){
        dataListDiv.innerHTML = data.map(item => `
                <tr>
                    <td>${item.id}</td>
                    <td>${item.name}</td>
                    <td>${item.nick}</td>
                    <td>${item.birthdata}</td>
                    <td>${item.sex}</td>
                    <td>${item.iphone}</td>
                    <td><button type="button" lay-on="page" onclick="look(${item.id})" id='look'>查看</button> 
                    <button onclick="bji(${item.id})" id='look'>编辑</button> 
                    <button class="del" onclick="del(${item.id})">删除</button></td>
                </tr>`)
            .join("");
        }app()
    }
    function sou() {
    var inp=document.querySelector('.content').value
    console.log(inp);
    console.log(5);
    $.ajax({
        type: 'get',
        url: 'http://47.94.4.201/index.php/index/teacher/getsearch',
        data: {
            name: inp
        },
        dataType: 'json',
        success: (arr) => {
            var a = arr.data.data
            console.log(a);
            // fu(a)
            app()
            fetchData(1)
        },
        error: (e) => {
            console.log(e);
        }
    })
    
}
fetchData(1); // 初始化加载第一页